<template>
  <div id="subpage">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <b>权限设置</b>
        </el-breadcrumb-item>
        <el-breadcrumb-item>菜单列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
    <div class="content">
      <div class="xcx-head">
        <div style="display: flex">
          <span>菜单列表</span>
        </div>
        <el-button @click="dialogOrderVisible = true" size="small" icon="el-icon-plus" type="success">添加菜单</el-button>
      </div>
      <div class="xcx-content">
        <!--列表-->
        <el-table border :data="tableData" style="width: 100%" row-key="menu_id" size="mini" v-loading="loading" default-expand-all :tree-props="{ children: 'child', hasChildren: 'hasChildren' }">
          <el-table-column prop="name" label="菜单名称" width="260"></el-table-column>
          <el-table-column prop="link" label="链接地址" width="260"></el-table-column>
          <el-table-column></el-table-column>
          <el-table-column align="center" label="状态" width="80">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="setStatus(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="display_order" label="排序" width="50"></el-table-column>
          <el-table-column align="center" label="操作" width="180">
            <template #default="scope">
              <el-button size="mini" icon="el-icon-edit" type="primary" plain @click="edit(scope.row.menu_id)">编辑</el-button>
              <el-button size="mini" icon="el-icon-delete" type="danger" plain @click="del_item(scope.row.menu_id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <el-dialog title="添加菜单" :visible.sync="dialogOrderVisible" :close-on-click-modal="false" width="35%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="上级栏目：" prop="parent_menu_id" label-width="100px">
          <el-select v-model="ruleForm.parent_menu_id" placeholder="请选择">
            <el-option label="顶级栏目" :value="0"></el-option>
            <el-option v-for="(item,indexs) in tableData" :key="indexs" :label="item.name" :value="item.menu_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="栏目名称：" prop="name" label-width="100px">
          <el-input v-model="ruleForm.name" placeholder="请输入栏目名称"></el-input>
        </el-form-item>
        <el-form-item label="页面路径：" prop="link" label-width="100px">
          <el-input v-model="ruleForm.link" placeholder="请输入页面路径"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标：" prop="icon" label-width="100px">
          <el-input v-model="ruleForm.icon" placeholder="请输入菜单图标"></el-input>
        </el-form-item>
        <el-form-item label="菜单排序：" prop="display_order" label-width="100px">
          <el-input v-model="ruleForm.display_order" placeholder="请输入菜单排序"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogOrderVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmDesc('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
  <script>
export default {
  data() {
    return {
      dialogOrderVisible: false,
      loading: true,
      id: "",
      tableData: [],
      page: 0,
      count: 0,
      limit: 30,
      ruleForm: {
        menu_id: 0,
        name: "",
        link: "",
        icon: "",
        parent_menu_id: 0,
        display_order: 0,
      },
      rules: {
        name: [{ required: true, message: "请填写名称", trigger: "blur" }],
      },
    };
  },
  //进入页面加载
  mounted: function () {
    var that = this;
    that.getList();
  },
  methods: {
    confirmDesc(formName) {
      const that = this;
      console.log(formName);
      that.$refs[formName].validate((valid) => {
        if (!valid) return that.$message.warning("请完整填写内容!");
        var formData = {};
        formData = that.ruleForm;
        if (that.id) {
          formData.id = that.id;
        }
        that.axios.post("/authMenu/beforeSave", formData).then(() => {
          that.$message.success(that.id ? "修改成功" : "添加成功");
          that.dialogOrderVisible = false;
          that.getList();
        });
      });
    },

    setStatus(row) {
      var that = this;
      that.axios
        .post("/authMenu/setStatus", {
          menu_id: row.menu_id,
          status: row.status,
        })
        .then((res) => {});
    },

    edit(ids) {
      var that = this;
      this.ruleForm.menu_id = ids;
      this.axios.post("/authMenu/detail", { menu_id: ids }).then((res) => {
        if (res.code == 200) {
          that.ruleForm = res.data;
        }
        that.loading = false;
      });
      this.dialogOrderVisible = true;
    },

    /**
     * 下一页
     */
    handleCurrentChange(currentPage) {
      var that = this;
      that.page = currentPage;
      that.getList();
    },

    del_item(ids) {
      let that = this;
      this.$confirm("此操作不可逆, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          that.axios
            .post("/lmcolumn/destroy", {
              ids: ids,
            })
            .then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: res.msg,
                });
                that.getList();
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            });
        })
        .catch(() => {});
    },

    /**
     * 获取列表
     */
    getList() {
      let that = this;
      let formData = {};
      var curr_page = sessionStorage.getItem("curr_page");
      that.page = curr_page ? curr_page - 0 : that.page;
      formData.page = that.page;
      formData.limit = that.limit;
      that.axios.post("/authMenu/index", formData).then((res) => {
        if (res) {
          that.tableData = res.data;
        }
        that.loading = false;
      });
    },
  },
};
</script>
  <style>
</style>